#include "ViewGame.h"

ViewGame::ViewGame() :
    gameEng(GameEngine::getInstance()),
    rend(RenderManager::getInstance()),
    globals(GlobalStates::getInstance())
{
    timerID=0;
    tbPrevX=0;
    tbPrevY=0;
    xDelta=0;
    yDelta=0;
    accX=0;
    accY=0;
}

View* ViewGame::getInstance(){
    static View* inst=0;
    if(!inst)return inst=(View*)(new ViewGame());
    else return inst;
}

void ViewGame::resize(int width, int height){
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //gluPerspective(90,ar,2,256);
    gluPerspective(60,ar,2,240);
    //glFrustum(-0.05,0.05,-0.05,0.05,0.02,256);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

void ViewGame::display(){
    static const GLfloat light_position[] = { 20.0f, 128.0f, 300.0f, 0.0f };
    rend->Camera.lookAt(gameEng->charCon.getCharacter()->geometry.center,10);

    rend->Camera.panRight(accX*ROT_SPEED);
    rend->Camera.panDown(accY*ROT_SPEED);

    accX*=(1-ROT_SPEED);
    accY*=(1-ROT_SPEED);

    glPushMatrix();

    glRotatef(rend->Camera.getRotY(),1,0,0);
    glRotatef(rend->Camera.getRotX(),0,1,0);
    glTranslatef(-rend->Camera.getEyeX(),-rend->Camera.getEyeY(),-rend->Camera.getEyeZ());

    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    rend->Render();
    gameEng->render();
    glPopMatrix();
}

void ViewGame::overlayDisplay(){
    glPushMatrix();
    gameEng->renderOverlay();
    glPopMatrix();
}

void ViewGame::init(){
    timerID=GLWindow::getInstance()->startTimer(1000/FPS);
    tbPrevX=0;
    tbPrevY=0;
    xDelta=0;
    yDelta=0;
    accX=0;
    accY=0;
    gameEng->initSession();
}

void ViewGame::destroy(){
    GLWindow::getInstance()->killTimer(timerID);
    rend->Camera.reset();
    gameEng->resetSession();
}

Result ViewGame::mousePressEvent(QMouseEvent *event){
    return Result(true);
}

Result ViewGame::mouseMoveEvent(QMouseEvent *event){
    float _x= (float)(globals->getScreenWidth() - event->x()*2)/globals->getScreenWidth();
    float _y= (float)(globals->getScreenHeight() - event->y()*2)/globals->getScreenHeight();

    _x = CLAMP(_x,-1.0,1.0);
    _y = CLAMP(_y,-1.0,1.0);

    float xAngle = asin(_x/1.0)*2.5;
    float yAngle = asin(_y/1.0)*2.5;

    xDelta = -TO_ANGLE(xAngle - tbPrevX);
    yDelta = -TO_ANGLE(yAngle - tbPrevY);

    accX+=xDelta;
    accY+=yDelta;

    tbPrevX = xAngle;
    tbPrevY = yAngle;
    return Result(true);
}

Result ViewGame::mouseReleaseEvent(QMouseEvent *event){
    return Result(true);
}

Result ViewGame::keyPressEvent(QKeyEvent *event){
    switch(event->key()) {
    case Qt::Key_W:
        gameEng->charCon.invokeCharMoveForward();
        break;
    case Qt::Key_S:
        gameEng->charCon.invokeCharMoveBackward();
        break;
    case Qt::Key_A:
        gameEng->charCon.invokeCharMoveLeft();
        break;
    case Qt::Key_D:
        gameEng->charCon.invokeCharMoveRight();
        break;
    case Qt::Key_E:
        gameEng->charCon.invokeCharMoveJump();
        break;
    case Qt::Key_Q:
        gameEng->invokeActivateEvent();
        break;

    default:
        //event->ignore();
        break;
    }
    return Result(true);
}

Result ViewGame::keyReleaseEvent(QKeyEvent *event){
    return Result(true);
}

void ViewGame::timerEvent(QTimerEvent *event){
}
